﻿using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using RG.Common.WPF.Commands;

namespace RG.Common.WPF.Controls
{
    public class MTabPanel : TabPanel
    {

        public ObservableCollection<UIElement> FrozenChildrens { get; set; }
        public ObservableCollection<UIElement> ScrollingChildrens { get; set; }


        public ICommand FreezeChildren { get; set; }

        public MTabPanel()
        {
            FrozenChildrens = new ObservableCollection<UIElement>();
            ScrollingChildrens = new ObservableCollection<UIElement>();
            FreezeChildren = new DelegateCommand<UIElement>(DoFreeze);
        }


        public void DoFreeze(UIElement tab)
        {
            if (FrozenChildrens.Count < 3)
            {
                FrozenChildrens.Add(tab);
                ScrollingChildrens.Remove(tab);
            }
        }
    }

    public class ScrollbarOnFarLeftConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return false;
            return ((double)value > 0);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class ScrollbarOnFarRightConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values == null) return false;
            if (values[0] == null || values[1] == null || values[2] == null) return false;
            if (values[0].Equals(double.NaN) || values[1].Equals(double.NaN) || values[2].Equals(double.NaN)) return false;

            double dblHorizontalOffset;
            double dblViewportWidth;
            double dblExtentWidth;

            double.TryParse(values[0].ToString(), out dblHorizontalOffset);
            double.TryParse(values[1].ToString(), out dblViewportWidth);
            double.TryParse(values[2].ToString(), out dblExtentWidth);

            bool fOnFarRight = Math.Round((dblHorizontalOffset + dblViewportWidth), 2) < Math.Round(dblExtentWidth, 2);
            return fOnFarRight;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
